package com.app.xinfadi.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.xinfadi.R;
import com.app.xinfadi.util.DateTimeUtils;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.CalendarPickerView.OnDateRangeSelectsListener;
import com.squareup.timessquare.CalendarPickerView.OnDateSelectedListener;
import com.squareup.timessquare.CalendarPickerView.SelectionMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CalendarPickerViewPopup extends PopupWindow {
	private Context context;
	private View calendar_picker_view_popup;
	private View calendarLinLay;
	private CalendarPickerView calendar_view;
	private OnDateSelectedListener onDateSelectedListener;
	private OnMyDateSelectedListener onMyDateSelectedListener;
	private OnMyDateRangeSelectsListener onMyDateRangeSelectsListener;
	private TextView selectedTimeTv;
	private TextView escort_persons_appointment_btn;
	private Type type;
	private View rangSelectLinLay;
	private View selectedTimeLinLay;
	private TextView startTimeTv;
	private TextView endTimeTv;
	private SimpleDateFormat sdf;

	public CalendarPickerViewPopup(Type type, Context context) {
		this.context = context;
		this.type = type;
		calendar_picker_view_popup = LayoutInflater.from(this.context).inflate(R.layout.calendar_picker_view_popup,
				null);
		initCalenderView();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		int screenWidth = wm.getDefaultDisplay().getWidth();// 手机屏幕的宽度
		int screenHeight = wm.getDefaultDisplay().getHeight();// 手机屏幕的高度
		this.setContentView(calendar_picker_view_popup);
		this.setWidth((int)(screenWidth*0.8));
		this.setHeight((int)(screenHeight*0.8));
		this.setFocusable(true);
		this.setOutsideTouchable(false);
		// this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
	}

	private void initCalenderView() {
		sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

		calendarLinLay = calendar_picker_view_popup.findViewById(R.id.calendarLinLay);
		calendar_view = (CalendarPickerView) calendar_picker_view_popup.findViewById(R.id.calendar_view);
		selectedTimeTv = (TextView) calendar_picker_view_popup.findViewById(R.id.selectedTimeTv);
		selectedTimeTv.setText(sdf.format(DateTimeUtils.getNowDate()));
		selectedTimeLinLay = calendar_picker_view_popup.findViewById(R.id.selectedTimeLinLay);
		rangSelectLinLay = calendar_picker_view_popup.findViewById(R.id.rangSelectLinLay);
		startTimeTv = (TextView) calendar_picker_view_popup.findViewById(R.id.startTimeTv);
		endTimeTv = (TextView) calendar_picker_view_popup.findViewById(R.id.endTimeTv);
		escort_persons_appointment_btn = (TextView) calendar_picker_view_popup
				.findViewById(R.id.escort_persons_appointment_btn);
		Calendar nextYear = Calendar.getInstance();
		nextYear.add(Calendar.YEAR, 1);

		Calendar lastYear = Calendar.getInstance();
		lastYear.add(Calendar.YEAR, -1);

		Calendar nowYear = Calendar.getInstance();
		Calendar fiveMouthAgoYear = Calendar.getInstance();

		fiveMouthAgoYear.add(Calendar.MONTH, 5);

		rangSelectLinLay.setVisibility(View.GONE);
		selectedTimeLinLay.setVisibility(View.GONE);
		switch (type) {
		case SingleSelect:
			selectedTimeLinLay.setVisibility(View.VISIBLE);
			calendar_view.init(nowYear.getTime(), fiveMouthAgoYear.getTime()).inMode(SelectionMode.SINGLE);
			break;
		case RangeSelect:
			calendar_view.init(nowYear.getTime(), fiveMouthAgoYear.getTime()).inMode(SelectionMode.RANGE);
			rangSelectLinLay.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
		calendar_view.setOnDateSelectedListener(new OnDateSelectedListener() {

			@Override
			public void onDateUnselected(Date date) {
			}

			@Override
			public void onDateSelected(Date date) {
				selectedTimeTv.setText(sdf.format(date));
				// home_whole_chaperonage_hospitalizeTime.setText(sdf.format(date));
				// appointmentBean.start_time = sdf.format(date);
				// appointmentBean.end_time = sdf.format(date);
				// home_whole_chaperonage_hospitalizeTime.setText(sdf.format(date));
				if (onMyDateSelectedListener != null) {
					onMyDateSelectedListener.onDateSelected(date);
				}
			}
		});
		calendar_view.setOnDateRangeSelectsListener(new OnDateRangeSelectsListener() {

			@Override
			public void onDateSelect(Date start, Date end) {
				startTimeTv.setText(sdf.format(start));
				endTimeTv.setText(sdf.format(end));
				if (onMyDateRangeSelectsListener != null) {
					onMyDateRangeSelectsListener.onDateSelect(start, end);
				}
			}

			@Override
			public void onDateCancelSelect() {
				if (onMyDateRangeSelectsListener != null) {
					onMyDateRangeSelectsListener.onDateCancelSelect();
				}

			}
		});

		// appointmentBean.start_time =
		// sdf.format(DateTimeUtils.getNowDate());
		// appointmentBean.end_time =
		// sdf.format(DateTimeUtils.getNowDate());
		// home_whole_chaperonage_hospitalizeTime.setText(sdf.format(DateTimeUtils.getNowDate()));

		escort_persons_appointment_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

	}

	public interface OnMyDateSelectedListener {
		/**
		 * 单个日期选择
		 *
		 * @param date
		 */
		public void onDateSelected(Date date);
	}

	/**
	 * 时间区间选择
	 *
	 * @Function: TODO <br/>
	 * @date 2015-6-3 下午6:50:57 <br/>
	 * @author WuTengFei <br/>
	 *
	 */
	public interface OnMyDateRangeSelectsListener {
		/**
		 *
		 * @param start
		 *            开始时间
		 * @param end
		 *            结束时间
		 */
		public void onDateSelect(Date start, Date end);

		/**
		 * 取消range选择
		 */
		public void onDateCancelSelect();
	}

	public OnMyDateSelectedListener getOnMyDateSelectedListener() {
		return onMyDateSelectedListener;
	}

	public void setOnMyDateSelectedListener(OnMyDateSelectedListener onMyDateSelectedListener) {
		this.onMyDateSelectedListener = onMyDateSelectedListener;
	}

	public OnMyDateRangeSelectsListener getOnMyDateRangeSelectsListener() {
		return onMyDateRangeSelectsListener;
	}

	public void setOnMyDateRangeSelectsListener(OnMyDateRangeSelectsListener onMyDateRangeSelectsListener) {
		this.onMyDateRangeSelectsListener = onMyDateRangeSelectsListener;
	}

	public enum Type {
		SingleSelect(0), RangeSelect(1);
		Type(int type) {
			this.type = type;
		}

		public int type;
	}

}
